﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Text.RegularExpressions;



namespace BreakerControlSoft
{


    public static class GLOBAL
    {
        public const int CMD_SUCCESS = 0;
        public const int CMD_FAIL = -1;
        public const int CMD_TIMEOUT = -2;
        public const int CMD_CRC_ERROR = -3;
        public const int CMD_EXCEPTION = -4;

        public const int USER = 0;
        public const int ADMIN = 1;
        public static int CurUser = USER;

        public static int Sensor_addr;

        public static SerialPort sp = null;//声明一个串口类

        public static bool isOpen = false;//打开串口标志位
        public static bool isSetProperty = false;//属性设置标志位

        //public static bool bIsLoopForever = false;
        //public static bool bIsLoopSend = false;  //是否循环发送
        //public static int FisrtDelayTime = 30;
        public static int C2OTime = 30;
        public static int O2CTime = 30;
        public static int IntvalTime = 30;

        public static int CPulseTime;
        public static int OPulseTime;
        public static int CTime;
        public static int OTime;
        //public static int LoopCnt = 10;

        //public static float[,] Calidata = new float[7,6];

        public static uint crc16_modbus(byte[] modbusdata, uint Length)//Length为modbusdata的长度 
        { 
            uint i, j; 
            uint crc16 = 0xFFFF;
            for (i = 0; i < Length; i++) 
            { 
                crc16 ^= modbusdata[i]; // CRC = BYTE xor CRC  
                for (j = 0; j < 8; j++) 
                { 
                    if ((crc16 & 0x01) == 1) //如果CRC最后一位为1右移一位后carry=1则将CRC右移一位后再与POLY16=0xA001进行xor运算 
                        crc16 = (crc16 >> 1) ^ 0xA001; 
                    else                     //如果CRC最后一位为0则只将CRC右移一位 
                        crc16 = crc16 >> 1; 
                } 
            } 
            return crc16; 
        }

        public static bool IsInteger(string value)
        {
            string pattern = @"^[0-9]*[1-9][0-9]*$";
            return Regex.IsMatch(value, pattern);
        }

        /// <summary>
        /// 判断一个字符串是否为合法数字(指定整数位数和小数位数)
        /// </summary>
        /// <param name="s">字符串</param>
        /// <param name="precision">整数位数</param>
        /// <param name="scale">小数位数</param>
        /// <returns></returns>
        public static bool IsFloatValid(string s, int precision, int scale)
        {
            if ((precision == 0) && (scale == 0))
            {
                return false;
            }
            string pattern = @"(^\d{1," + precision + "}";
            if (scale > 0)
            {
                pattern += @"\.\d{0," + scale + "}$)|" + pattern;
            }
            pattern += "$)";
            return Regex.IsMatch(s, pattern);
        }

        public static bool bIsInputTextNumlegal(string strinput)
        {
            bool bIsInputlegal = true;
            for (int i = 0; i < strinput.Length; i++)
            {
                if (strinput[i] >= '0' && strinput[i] <= '9')
                {
                    ;
                }
                else
                {
                    bIsInputlegal = false;
                    break;
                }
            }

            if (strinput.Length > 1)
            {
                if (strinput[0] == '0')
                {
                    bIsInputlegal = false;
                }
            }

            if (strinput.Length == 0) bIsInputlegal = false;

            return bIsInputlegal;
        }
    }
}
